// import Taro from '@tarojs/taro'
import { isNull } from 'lodash'
import { UPDATA_CITY, UPDATA_LOCATION, CITYLIST, UPDATA_STORELOCATION, IS_OPENLOCATION, SET_CUSTOM_ADDRESS,UPDATA_CITYLOCATION,UPDATA_LONGITUDE_LATITUDE } from './action-types'

interface State {
    latAndLng: GlobalStore.LatAndLng
    address: object | null | boolean
    storeLocation: IStore | null
		cityLocation:any,
    cityList: any
    cityName: null | string | undefined
    isOpenLocation: boolean
    customAddress: boolean
		currentlongitudeLatitude :any
}

const INITIAL_STATE: State = {
    // 当前区域经纬度
    latAndLng: {
        latitude: 0,
        longitude: 0
    },
    // 当前区域信息
    address: null,
    // 门店信息
    storeLocation: null,
		// 城市信息
    cityLocation: null,
    // 城市列表
    cityList: null,
    // 城市名
    cityName: null,
    // 位置授权状态（true: 已开启，false: 未开启）
    isOpenLocation: false,
    // 手动切换区域信息状态
    customAddress: false,
		// 用户位置当前当前经纬度信息
		currentlongitudeLatitude:null
}
export default function location(state = INITIAL_STATE, action) {
    switch (action.type) {
			// 更新门店首页
			case UPDATA_STORELOCATION:
				return {
						...state,
						storeLocation: action.storeLocation
				}
        // 更新门店首页
        case UPDATA_STORELOCATION:
            return {
                ...state,
                storeLocation: action.storeLocation
            }
				// 更新定位数据
				case UPDATA_LONGITUDE_LATITUDE:
				return {
					...state,
					currentlongitudeLatitude:action.currentlongitudeLatitude
				}
				// 更新城市数据
				case UPDATA_CITYLOCATION:
					return {
							...state,
							cityLocation: action.cityLocation
					}
        case UPDATA_LOCATION:
            let newAdress = {}
            const { address } = action
            if (address) {
                const { location, city } = address
                newAdress = {
                    latAndLng: {
                        latitude: location.lat,
                        longitude: location.lng
                    },
                    cityName: city
                }
            }
            return {
                ...state,
                ...newAdress,
                address
            }
        case CITYLIST:
            return {
                ...state,
                cityList: action.cityList
            }
        case UPDATA_CITY:
            return {
                ...state,
                cityName: action.cityName
            }
        // 位置授权状态（true: 已开启，false: 未开启）
        case IS_OPENLOCATION:
            return {
                ...state,
                isOpenLocation: action.isOpenLocation
            }
        case SET_CUSTOM_ADDRESS:
            return {
                ...state,
                customAddress: action.customAddress
            }
        default:
            return state
    }
}
